package com.hhsj.design.pullableview.scrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;

import androidx.recyclerview.widget.RecyclerView;

import com.hhsj.design.pullableview.view.DefaultFooterLineView;
import com.hhsj.design.pullableview.view.DefaultLoadMoreView;
import com.hhsj.design.pullableview.view.DefaultRefreshView;
import com.hhsj.design.pullableview.FooterBackToNormalRunnable;
import com.hhsj.design.pullableview.FooterGoingToLoadMoreRunnable;
import com.hhsj.design.pullableview.HeaderBackToNormalRunnable;
import com.hhsj.design.pullableview.HeaderGoingToRefreshRunnable;
import com.hhsj.design.pullableview.view.IFooterLineView;
import com.hhsj.design.pullableview.view.ILoadMoreView;
import com.hhsj.design.pullableview.ability.IPullAble;
import com.hhsj.design.pullableview.view.IRefreshView;


public class RefreshScrollViewPro extends ScrollView implements IPullAble {

    /**
     * 1.如果listview滑出屏幕的距离为0时，上拉时不拦截处理，下拉时拦截处理，header下面的view慢慢显示出来
     * 2.如果listview滑出屏幕的距离不为0时，执行其自身的滑动
     * 3.如果正在向下显示header下面的view时，上滑view也向上隐藏，隐藏完继续listview自身的滑动
     */
    private LinearLayout mContainer;

    private LinearLayout mHeaderParent;
    private LinearLayout mHeaderLayout;
    private FrameLayout mRefreshParent;
    private IRefreshView mIRefreshView;


    private LinearLayout mFooterParent;
    private LinearLayout mFooterLayout;

    private FrameLayout mFooterLineParent;
    private IFooterLineView mIFooterLineView;

    private FrameLayout mLoadMoreParent;
    private ILoadMoreView mILoadMoreView;

    private int mTouchSlop;

    //刷新视图的默认测量高度
    private int mRefreshViewHeight;

    //需要刷新的时候的刷新视图的高度
    private int mCanRefreshHeight;

    //上拉加载等多的视图的高度
    private int mLoadMoreViewHeight;


    //需要继续刷新的时候刷新视图的高度
    private int mContinuePullHeight;

    private int mCanLoadMoreHeight;//可执行上拉加载更多时的高度阈值

    private float mLastX, mLastY;

    private boolean mIsDrag;

    private boolean isFooterLineShowing = false;


    //刷新控件的状态
    private HeaderState mHeaderState = HeaderState.NORMAL;
    //加载更多的状态
    private FooterState mFooterState = FooterState.NORMAL;

    private ViewGroup.LayoutParams headerParams;
    private HeaderBackToNormalRunnable mBackNormalStateRunnable;
    private HeaderGoingToRefreshRunnable mBackRefreshStateRunnable;

    private ViewGroup.LayoutParams footerParams;
    private FooterBackToNormalRunnable mFooterBackNormalStateRunnable;
    private FooterGoingToLoadMoreRunnable mFooterBackLoadMoreStateRunnable;

    private PullAbleListener mListener;
    private int refreshViewMode = REFRESH_VIEW_MODE_TOP;

    private boolean supportElastic = true;//是否支持弹性
    private boolean supportContinuePull = false;//支持继续下拉
    private boolean supportRefresh = false;//是否支持刷新
    private boolean supportLoadMore = false;//是否支持加载更多刷新
    private boolean supportFooterLine = false;//是否支持底线


    public RefreshScrollViewPro(Context context) {
        this(context, null);
    }

    public RefreshScrollViewPro(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshScrollViewPro(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        mContainer = new LinearLayout(context);
        mContainer.setOrientation(LinearLayout.VERTICAL);
        addView(mContainer);
        initHeader();
        initFooter();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int count = getChildCount();
        if (count != 1 || !(getChildAt(0) instanceof LinearLayout)) {
            throw new IllegalArgumentException("scrollview must be one LinearLayout");
        }
    }

    //设置头部
    @Override
    public void addHeaderView(View header) {
        if (header != null) {
            mHeaderLayout.addView(header);
        }
    }

    @Override
    public void addFooterView(View footer) {
        if (footer != null) {
            mFooterLayout.addView(footer);
        }
    }

    private void initHeader() {

        //添加头部布局
        mHeaderParent = new LinearLayout(getContext());
        mHeaderParent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        mHeaderParent.setOrientation(LinearLayout.VERTICAL);
        mContainer.addView(mHeaderParent, 0);

        //头部布局设立可添加的头部布局 用LinearLayout布局
        mHeaderLayout = new LinearLayout(getContext());
        mHeaderLayout.setBackgroundColor(0x00000000);
        mHeaderLayout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
        mHeaderLayout.setOrientation(LinearLayout.VERTICAL);
        mHeaderParent.addView(mHeaderLayout);

        //设立可刷新布局 用FrameLayout布局
        mRefreshParent = new FrameLayout(getContext());
        mRefreshParent.setBackgroundColor(0x00000000);
        mRefreshParent.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 0));

        //根据可刷新布局的位置模式，设置布局的位置。
        if (refreshViewMode == REFRESH_VIEW_MODE_TOP) {
            mHeaderParent.addView(mRefreshParent, 0);
        } else {
            mHeaderParent.addView(mRefreshParent);
        }
    }

    private void initFooter() {
        //添加底部布局
        mFooterParent = new LinearLayout(getContext());
        mFooterParent.setOrientation(LinearLayout.VERTICAL);
        mFooterParent.setGravity(Gravity.CENTER_HORIZONTAL);
        mFooterParent.setLayoutParams(new ListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
        mContainer.addView(mFooterParent);

        //真的意义上的footer
        mFooterLayout = new LinearLayout(getContext());
        mFooterLayout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
        mFooterLayout.setOrientation(LinearLayout.VERTICAL);
        mFooterParent.addView(mFooterLayout);

        //底线和刷新布局
        mFooterLineParent = new FrameLayout(getContext());
        mFooterLineParent.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 0));
        mFooterParent.addView(mFooterLineParent);


        //底线布局
        mFooterLineParent = new FrameLayout(getContext());
        mFooterLineParent.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mFooterParent.addView(mFooterLineParent);

        //刷新布局
        mLoadMoreParent = new FrameLayout(getContext());
        mLoadMoreParent.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 0));
        mFooterParent.addView(mLoadMoreParent);
    }

    /**
     * 当下拉的时候，需要拦截
     */
    private boolean isVerticalDrag(MotionEvent ev) {
        if (ev == null) {
            return false;
        }
        return Math.abs(mLastX - ev.getX()) < Math.abs(mLastY - ev.getY())
                && Math.abs(mLastY - ev.getY()) > mTouchSlop;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (mHeaderState == HeaderState.TO_REFRESH
                || mHeaderState == HeaderState.REFRESHING
                || mHeaderState == HeaderState.TO_NORMAL) {
            return false;
        }

        if (mFooterState == FooterState.TO_LOAD
                || mFooterState == FooterState.LOADING
                || mFooterState == FooterState.TO_NORMAL) {
            return false;
        }
        if ((!supportRefresh || mIRefreshView == null)
                && (!supportLoadMore || mILoadMoreView == null)) {
            return super.onInterceptTouchEvent(ev);
        }
        int action = ev.getActionMasked();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mLastX = ev.getX();
                mLastY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (isVerticalDrag(ev)) {
                    return true;
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        Log.e("HHSJ_STATE", "Header " + mHeaderState.toString() + " Footer " + mFooterState.toString());

        if (mHeaderState == HeaderState.TO_REFRESH
                || mHeaderState == HeaderState.REFRESHING
                || mHeaderState == HeaderState.TO_NORMAL) {
            return false;
        }

        if (mFooterState == FooterState.TO_LOAD
                || mFooterState == FooterState.LOADING
                || mFooterState == FooterState.TO_NORMAL) {
            return false;
        }
        //需要处理header view里面的显示的话，要设置param，否则给父类自己来处理
        if (!supportElastic &&
                (!supportRefresh || mIRefreshView == null)
                && (!supportLoadMore || mILoadMoreView == null)) {
            return super.onTouchEvent(ev);
        }

        //刷新布局的高度
        if (mRefreshViewHeight <= 0 && mIRefreshView != null) {
            mRefreshViewHeight = mIRefreshView.getRefreshHeight();
        }

        mCanRefreshHeight = mRefreshViewHeight;

        //继续下拉
        if (supportContinuePull) {
            mContinuePullHeight = mCanRefreshHeight + 240;
        }

        if (mLoadMoreViewHeight <= 0 && mILoadMoreView != null) {
            mLoadMoreViewHeight = mILoadMoreView.getLoadMoreHeight();
        }

        mCanLoadMoreHeight = mLoadMoreViewHeight;

        int action = ev.getActionMasked();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mLastX = ev.getX();
                mLastY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (!mIsDrag && isVerticalDrag(ev)) {
                    mIsDrag = true;
                }
                if (mIsDrag) {
                    if (getScrollY() == 0
                            && ev.getY() > mLastY) {
                        if ((supportRefresh && mIRefreshView != null)) {
                            moveHeaderLayout(ev.getY() - mLastY);
                            return true;
                        } else if (supportElastic) {
                            moveHeaderLayout(ev.getY() - mLastY);
                            return true;
                        }
                    } else if (isSlideToBottom() && ev.getY() < mLastY) {
                        if ((supportLoadMore && mILoadMoreView != null)) {
                            moveFooterLayout(mLastY - ev.getY());
                            //这里的事件一定要拦截，不能给到ScrollView父类处理，因为父类处理的时候会使得canScrollVertical(1)变成ture，
                            // 从而在UP的时候无法执行回弹等操作
                            return true;
                        } else if (supportElastic) {
                            moveFooterLayout(mLastY - ev.getY());
                            //这里的事件一定要拦截，不能给到ScrollView父类处理，因为父类处理的时候会使得canScrollVertical(1)变成ture，
                            // 从而在UP的时候无法执行回弹等操作
                            return true;
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                mIsDrag = false;
                if (getScrollY() == 0) {
                    if ((supportRefresh && mIRefreshView != null)) {
                        if (headerParams == null) {
                            headerParams = mRefreshParent.getLayoutParams();
                        }
                        if (headerParams.height <= 0) {
                            break;
                        }
                        //松开手指，后如果高度不够，回弹，高度够，执行刷新
                        boolean canRefresh = headerParams.height >= mCanRefreshHeight;
                        if (!canRefresh) {
                            headerBackNormal(headerParams.height);
                        } else {
                            boolean canContinuePull = supportContinuePull
                                    && headerParams.height >= mContinuePullHeight;
                            if (canContinuePull) {
                                if (null != mListener && mListener.onContinuePullAction()) {
                                    headerBackNormal(headerParams.height);
                                }
                            } else {
                                startRefreshHeight(headerParams.height, mCanRefreshHeight);
                            }
                        }
                    } else if (supportElastic) {
                        headerBackNormal(headerParams.height);
                    }
                } //上拉加载更多，换个思路，用间距
                else if (isSlideToBottom()) {
                    //最后一个可见
                    if ((supportLoadMore && mILoadMoreView != null)) {
                        if (footerParams == null) {
                            footerParams = mLoadMoreParent.getLayoutParams();
                        }
                        if (footerParams.height == 0) {
                            break;
                        }
                        //松开手指，后如果高度不够，回弹，高度够，执行刷新
                        boolean canLoadMore = footerParams.height >= mCanLoadMoreHeight;
                        if (!canLoadMore) {
                            footerBackNormal(footerParams.height);
                        } else {
                            startLoadMoreHeight(footerParams.height, mCanLoadMoreHeight);
                        }
                    } else if (supportElastic) {
                        footerBackNormal(footerParams.height);
                    }
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    private void headerBackNormal(int currentHeight) {
        int duration = (int) Math.min(currentHeight / (float) mCanRefreshHeight * 500, 200);
        if (mBackNormalStateRunnable == null) {
            mBackNormalStateRunnable = new HeaderBackToNormalRunnable(mRefreshParent) {

                @Override
                public void onHeaderBackNormalStart() {
                    mHeaderState = HeaderState.TO_NORMAL;
                }

                @Override
                protected void onHeaderBackNormalCompleted() {
                    mHeaderState = HeaderState.NORMAL;
                    if (mIRefreshView != null) {
                        mIRefreshView.updateText("下拉刷新");
                        mIRefreshView.stopAnim();
                    }
                }

                @Override
                public void updateHeaderLayoutHeight(int movedHeight) {
                    updateRefreshLayoutHeight(movedHeight);
                }
            };
        }
        mBackNormalStateRunnable.start(duration);
    }

    private void footerBackNormal(int currentHeight) {
        footerBackNormalImmediately(currentHeight, false);
    }

    private void footerBackNormalImmediately(int currentHeight, boolean immediately) {
        if (immediately) {
            if (mILoadMoreView != null) {
                mILoadMoreView.updateText("上拉加载更多");
                mILoadMoreView.stopAnim();
            }
            updateLoadLayoutHeight(0);
            mFooterState = FooterState.NORMAL;
        } else {
            int duration = (int) Math.min(currentHeight / (float) mCanLoadMoreHeight * 500, 300);
            if (mFooterBackNormalStateRunnable == null) {
                mFooterBackNormalStateRunnable = new FooterBackToNormalRunnable(mFooterLineParent) {

                    @Override
                    public void onFooterBackNormalStart() {
                        mFooterState = FooterState.TO_NORMAL;
                    }

                    @Override
                    protected void onFooterBackNormalCompleted() {
                        mFooterState = FooterState.NORMAL;
                        if (mILoadMoreView != null) {
                            mILoadMoreView.updateText("上拉加载更多");
                            mILoadMoreView.stopAnim();
                        }
                    }

                    @Override
                    public void updateFooterLayoutHeight(int movedHeight) {
                        updateLoadLayoutHeight(movedHeight);
                    }
                };
            }
            mFooterBackNormalStateRunnable.start(duration);
        }
    }

    /**
     * 弹到开始刷新的高度
     */
    private void startRefreshHeight(int currentHeight, int targetHeight) {
        if (mBackRefreshStateRunnable == null) {
            mBackRefreshStateRunnable = new HeaderGoingToRefreshRunnable(
                    mRefreshParent,
                    targetHeight) {
                @Override
                public void onToRefresh() {
                    mHeaderState = HeaderState.TO_REFRESH;
                }

                @Override
                public void doRefresh() {
                    mHeaderState = HeaderState.REFRESHING;
                    mIRefreshView.updateText("正在刷新");
                    mIRefreshView.startAnim();
                    if (mListener != null) {
                        mListener.doRefresh();
                    }
                }


                @Override
                public void updateLayoutHeight(int movedHeight) {
                    updateRefreshLayoutHeight(movedHeight);
                }
            };
        }
        mBackRefreshStateRunnable.start(500);
    }

    /**
     * 当前高度回弹到开始刷新的高度
     *
     * @param currentHeight 当前高度
     * @param targetHeight  目标高度
     */
    private void startLoadMoreHeight(int currentHeight, int targetHeight) {
        if (mFooterBackLoadMoreStateRunnable == null) {
            mFooterBackLoadMoreStateRunnable = new FooterGoingToLoadMoreRunnable(
                    mFooterLineParent,
                    targetHeight) {

                @Override
                public void onToLoad() {
                    mFooterState = FooterState.TO_LOAD;
                    if (mILoadMoreView != null) {
                        mILoadMoreView.updateText("松开加载");
                    }
                }

                @Override
                public void doLoadMore() {
                    mFooterState = FooterState.LOADING;
                    if (mILoadMoreView != null) {
                        mILoadMoreView.updateText("正在加载");
                        mILoadMoreView.startAnim();
                    }
                    if (mListener != null) {
                        mListener.doLoadMore();
                    }
                }

                @Override
                public void updateLayoutHeight(int movedHeight) {
                    updateLoadLayoutHeight(movedHeight);
                }

            };
        }
        mFooterBackLoadMoreStateRunnable.start(500);
    }


    /**
     * 根据move事件y的距离更新刷新布局的layoutparam的height
     *
     * @param eventDeltaY move事件的y方向距离
     */
    private void moveHeaderLayout(float eventDeltaY) {
        float changeHeight = (float) Math.pow(eventDeltaY, 0.85);
        headerParams = mRefreshParent.getLayoutParams();
        headerParams.height = (int) changeHeight;
        if (supportRefresh && mIRefreshView != null) {
            if (supportContinuePull) {
                if (headerParams.height > mContinuePullHeight) {
                    mIRefreshView.updateText("继续下拉进入特卖场");
                } else if (headerParams.height > mCanRefreshHeight) {
                    mIRefreshView.updateText("松开刷新");
                } else {
                    mIRefreshView.updateText("下拉刷新");
                }
            } else {
                if (headerParams.height > mCanRefreshHeight) {
                    mIRefreshView.updateText("松开刷新");
                } else {
                    mIRefreshView.updateText("下拉刷新");
                }
            }
        }
        mRefreshParent.requestLayout();
    }

    /**
     * 根据move事件y的距离更新刷新布局的LayoutParams的height
     *
     * @param eventDeltaY move事件的y方向距离
     */
    private void moveFooterLayout(float eventDeltaY) {
        float changeHeight = (float) Math.pow(eventDeltaY, 0.85);

        mILoadMoreView.getLoadMoreView().setVisibility(VISIBLE);
        if (mIFooterLineView != null) {
            mIFooterLineView.getFootLineView().setVisibility(GONE);
        }
        footerParams = mLoadMoreParent.getLayoutParams();

        //保存上次
        int lastHeight = footerParams.height;

        footerParams.height = (int) changeHeight;
        if (footerParams.height > mCanLoadMoreHeight) {
            mILoadMoreView.updateText("松开加载更多");
        } else {
            mILoadMoreView.updateText("上拉加载更多");
        }
        mFooterLineParent.requestLayout();
        //由于高度变化了，导致canScrollVertically(1) 为true，所以需要先滑到底
        scrollBy(0, (int) (changeHeight - lastHeight));
    }


    /**
     * 更新刷新视图高度
     *
     * @param height
     */
    private void updateRefreshLayoutHeight(int height) {
        headerParams = mRefreshParent.getLayoutParams();
        headerParams.height = height;
        mRefreshParent.requestLayout();
    }

    /**
     * 更新刷新视图高度
     *
     * @param height
     */
    private void updateLoadLayoutHeight(int height) {
//        int footLineHeight = 0;
//        footerParams = mFooterLineLoadMoreParent.getLayoutParams();
//        if (height == 0 && supportFooterLine && isFooterLineShowing && mIFooterLineView != null) {
//            footLineHeight = mIFooterLineView.getFootLineView().getMeasuredHeight();
//        } else {
//
//        }
//        footerParams.height = Math.max(height, footLineHeight);
//        mFooterParent.requestLayout();

        if (height >= 0) {
            footerParams = mLoadMoreParent.getLayoutParams();
            footerParams.height = height;
            mFooterParent.requestLayout();
        }
        while (canScrollVertically(1)) {
            scrollBy(0, 1);
        }
    }


    @Override
    public void setElasticEnable(boolean elasticEnable) {
        supportElastic = elasticEnable;
    }

    @Override
    public boolean isElasticEnable() {
        return supportElastic;
    }

    @Override
    public void setPullDownAbleListener(PullDownAbleListener listener) {

    }

    @Override
    public void setContinuePullEnable(boolean continuePullEnable) {
        supportContinuePull = continuePullEnable;
    }

    @Override
    public boolean isContinuePullEnable() {
        return supportContinuePull;
    }

    @Override
    public void setRefreshEnable(boolean refreshEnable) {

        if (supportRefresh != refreshEnable) {
            supportRefresh = refreshEnable;
            if (!supportRefresh) {
                mRefreshParent.setMinimumHeight(0);
                mRefreshParent.removeAllViews();
                mRefreshParent.requestLayout();
                return;
            }
            if (mIRefreshView == null) {
                addRefreshView(null, 0);
                mRefreshParent.setMinimumHeight(1);
            } else {
                if (mIRefreshView.getRefreshView().getParent() != mRefreshParent) {
                    if (null != mIRefreshView.getRefreshView().getParent()) {
                        ((ViewGroup) mIRefreshView.getRefreshView().getParent())
                                .removeView(mIRefreshView.getRefreshView());
                    }
                    mRefreshParent.addView(mIRefreshView.getRefreshView());
                }
                mHeaderParent.requestLayout();
            }
        }
    }

    //添加刷新控件
    private void updateHeaderRefreshView() {
        if (!supportRefresh) {
            return;
        }
        if (mIRefreshView == null) {
            addRefreshView(null, 0);
        } else {
            if (mIRefreshView.getRefreshView() != null) {
                ((ViewGroup) mIRefreshView.getRefreshView().getParent()).removeView(mIRefreshView.getRefreshView());

                if (mRefreshParent.getChildCount() > 0) {
                    mRefreshParent.removeAllViews();
                }

                mRefreshParent.addView(mIRefreshView.getRefreshView());
                mHeaderParent.requestLayout();
            }
        }
    }

    @Override
    public boolean isRefreshEnable() {
        return supportRefresh;
    }


    @Override
    public void setLoadMoreEnable(boolean loadMoreEnable) {
        supportLoadMore = loadMoreEnable;
        if (supportLoadMore) {
            mFooterParent.setMinimumHeight(1);
        } else {
            mFooterParent.setMinimumHeight(0);
        }
        updateFooterLoadMoreView();
    }

    private void updateFooterLoadMoreView() {
        if (!supportLoadMore) {
            return;
        }
        if (mILoadMoreView == null) {
            setLoadMoreView(null, 0);
        } else {
            if (mILoadMoreView.getLoadMoreView() != null) {
                ((ViewGroup) mILoadMoreView.getLoadMoreView().getParent()).removeView(mILoadMoreView.getLoadMoreView());
                mRefreshParent.addView(mIRefreshView.getRefreshView());
                mHeaderParent.requestLayout();
            }
        }
    }

    @Override
    public boolean isLoadMoreEnable() {
        return supportLoadMore;
    }


    //设置刷新布局，只允许1个
    @Override
    public void addRefreshView(IRefreshView view, int refreshHeight) {
        if (!supportRefresh) {
            return;
        }

        if (mRefreshParent.getChildCount() > 0) {
            mRefreshParent.removeAllViews();
        }

        mIRefreshView = (view == null ? getDefaultRefreshView() : view);

        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mIRefreshView.getRefreshView().getLayoutParams();

        if (params == null) {
            throw new IllegalArgumentException("view must setLayoutParams with FrameLayout.LayoutParams");
        }

        if (params.height == RecyclerView.LayoutParams.WRAP_CONTENT || params.height == RecyclerView.LayoutParams.MATCH_PARENT) {
            params.height = refreshHeight;
        }

        params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
        mRefreshParent.addView(mIRefreshView.getRefreshView());
        mHeaderParent.requestLayout();
    }

    @Override
    public void setLoadMoreView(ILoadMoreView iLoadMoreView, int defaultFooterHeight) {
        if (!supportLoadMore) {
            return;
        }

        if (mLoadMoreParent.getChildCount() > 0) {
            mLoadMoreParent.removeAllViews();
        }
        mILoadMoreView = (null == iLoadMoreView ? getDefaultFooterView() : iLoadMoreView);
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)
                mILoadMoreView.getLoadMoreView().getLayoutParams();
        if (params == null) {
            throw new IllegalArgumentException("View must setLayoutParams with " +
                    "FrameLayout$LayoutParams");
        }

        if (params.height == ViewGroup.LayoutParams.MATCH_PARENT
                || params.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            params.height = defaultFooterHeight;
        }

        params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
        mLoadMoreParent.addView(mILoadMoreView.getLoadMoreView());
        mFooterParent.requestLayout();
    }

    @Override
    public void addFooterLineView(IFooterLineView iFooterLineView) {
        if (!supportFooterLine) {
            return;
        }
        mIFooterLineView = (null == iFooterLineView ? getDefaultFooterLineView() : iFooterLineView);
        mFooterLineParent.removeAllViews();
        mFooterLineParent.addView(mIFooterLineView.getFootLineView());
    }

    //设置位置
    @Override
    public void setRefreshViewMode(int mode) {
        if (mode != refreshViewMode && (mode == REFRESH_VIEW_MODE_MID || mode == REFRESH_VIEW_MODE_TOP)) {
            refreshViewMode = mode;
            resetHeaderRefreshViewPosition();
        }
    }

    private void resetHeaderRefreshViewPosition() {
        if (mHeaderParent == null || mHeaderParent.getChildCount() != 2) {
            return;
        }
        if (refreshViewMode == REFRESH_VIEW_MODE_TOP) {
            if (mHeaderParent.getChildAt(0) == mHeaderLayout) {
                mHeaderParent.removeView(mHeaderLayout);
                mHeaderParent.addView(mHeaderLayout);
            }
        } else {
            if (mHeaderParent.getChildAt(1) == mHeaderLayout) {
                mHeaderParent.removeView(mHeaderLayout);
                mHeaderParent.addView(mHeaderLayout, 0);
            }
        }
    }

    @Override
    public int getRefreshViewMode() {
        return refreshViewMode;
    }


    @Override
    public void setFooterLineEnable(boolean footerLineEnable) {
        if (supportFooterLine != footerLineEnable) {
            supportFooterLine = footerLineEnable;
            updateFooterLineView();
        }
    }

    /**
     * 更新底线布局
     * 条件：支持底线，是否显示底线
     */
    private void updateFooterLineView() {
        if (!supportFooterLine) {
            isFooterLineShowing = false;
            if (mIFooterLineView != null && mIFooterLineView.getFootLineView().getParent() == mFooterLineParent) {
                mFooterLineParent.removeView(mIFooterLineView.getFootLineView());
            }
            mIFooterLineView = null;
        } else {
            addFooterLineView(null);
        }
        mFooterLineParent.requestLayout();
    }

    @Override
    public boolean isFooterLineEnable() {
        return supportFooterLine;
    }

    @Override
    public void showFooterLine(boolean show) {
        if (supportFooterLine) {
            isFooterLineShowing = show;
            if (null != mIFooterLineView) {
                mIFooterLineView.getFootLineView().setVisibility(isFooterLineShowing ? VISIBLE : GONE);
            }
        }
    }

    @Override
    public boolean isFooterLineShowing() {
        return supportFooterLine && isFooterLineShowing;
    }

    @Override
    public void setFooterLineEnableAndShow() {

    }

    @Override
    public void setLoadMoreViewBgColor(int colorInt) {
        mLoadMoreParent.setBackgroundColor(colorInt);
    }

    @Override
    public void setLoadMoreDisableAndShowFooterLine() {
        if (supportLoadMore) {
            return;
        }
        //不支持
        if (!supportFooterLine) {
            return;
        }
        isFooterLineShowing = true;
    }

    @Override
    public IFooterLineView getFooterLineView() {
        return mIFooterLineView;
    }

    @Override
    public void setPullAbleListener(PullAbleListener listener) {
        mListener = listener;
    }

    @Override
    public void stopRefresh() {
        mIRefreshView.updateText("刷新完成");
        mIRefreshView.stopAnim();
        headerBackNormal(headerParams.height);
    }

    @Override
    public void stopLoadMore() {
        if (supportFooterLine) {
            if (mIFooterLineView != null) {
                mIFooterLineView.getFootLineView().setVisibility(VISIBLE);
            }
            if (mILoadMoreView != null) {
                mILoadMoreView.getLoadMoreView().setVisibility(GONE);
            }
            mLoadMoreParent.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
            mLoadMoreParent.requestLayout();
            mFooterState = FooterState.NORMAL;
        } else {
            if (mILoadMoreView != null) {
                mILoadMoreView.updateText("刷新完成");
                mILoadMoreView.stopAnim();
            }
            footerBackNormalImmediately(footerParams.height, true);
        }
    }


    private ILoadMoreView getDefaultFooterView() {
        return new DefaultLoadMoreView(getContext());
    }

    private IRefreshView getDefaultRefreshView() {
        return new DefaultRefreshView(getContext());
    }

    private IFooterLineView getDefaultFooterLineView() {
        return new DefaultFooterLineView(getContext());
    }

    boolean isSlideToBottom() {
        boolean isBottom = !canScrollVertically(1);
        Log.e("HHSJHHSJ", "ACTION_UP isBottom = " + isBottom);
        return isBottom;
    }


    public void addViewToContent(View view) {
        mContainer.addView(view, mContainer.getChildCount() - 1);
    }
}